package com.caiyi.tips.user.service.impl;

import com.caiyi.tips.core.Constant.RedisKeyConstant;
import com.caiyi.tips.core.VO.ResultVO;
import com.caiyi.tips.core.enums.ResultEnum;
import com.caiyi.tips.core.util.CheckUtil;
import com.caiyi.tips.core.util.DateUtil;
import com.caiyi.tips.user.DTO.SmsDTO;
import com.caiyi.tips.user.bean.SmsBean;
import com.caiyi.tips.user.exception.UserException;
import com.caiyi.tips.user.mapper.SmsMapper;
import com.caiyi.tips.user.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/*
 * @author Cliff.ZhouYang
 * @date 2017/11/9 15:15
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private SmsMapper smsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void checkSmsYzm(SmsBean smsBean) {
        log.info("校验短信验证码,phone=" + smsBean.getPhone() + ",smsType=" + smsBean.getYzmType() + " userType=" + smsBean.getUserType());
        int ret = smsMapper.updateMsgCheckCount(smsBean);
        if (ret != 1) {
            log.info("更新短信验证码校验次数失败 phone=" + smsBean.getPhone() + " smsType=" + smsBean.getYzmType() + " userType=" + smsBean.getUserType());
            throw new UserException(ResultEnum.SMSYZM_CHECK_ERROR.getCode(), "验证码错误");
        }

        ret = smsMapper.updateMsgVerify(smsBean);
        if (ret != 1) {
            log.info("短信验证码错误,phone=" + smsBean.getPhone() + " smsType=" + smsBean.getYzmType() + " userType=" + smsBean.getUserType());
            throw new UserException(ResultEnum.SMSYZM_CHECK_ERROR.getCode(), "验证码错误");
        }
        log.info("短信验证码正确,phone=" + smsBean.getPhone() + ",smsType=" + smsBean.getYzmType());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO sendYzm(SmsDTO smsDTO) {
        String phone = smsDTO.getPhone();
        String key = RedisKeyConstant.SEND_YZM0_CACHE_KEY_PREFIX + phone;
        //String lockKey = RedisKeyConstant.SEND_YZM0_LOCK_KEY_PREFIX + phone;
        SmsDTO cacheSmsDto = (SmsDTO) redisTemplate.opsForValue().get(key);
        if (cacheSmsDto == null) {
            cacheSmsDto = new SmsDTO();
            cacheSmsDto.setPhone(phone);
            cacheSmsDto.setCount(0);
            cacheSmsDto.setCreateDate(new Date());
        } else {
            if (cacheSmsDto.getCount() >= 10) {
                return ResultVO.failMsg("发送短信达今日上限10次");
            }
        }
        String random = CheckUtil.randomNum();
        smsDTO.setRandom(random);
        String sms = "【料到】您的验证码为" + random;
        smsDTO.setContent(sms);
        smsMapper.insertBindMsg(smsDTO);
        smsMapper.insertSms(smsDTO);

        cacheSmsDto.setCount(cacheSmsDto.getCount() + 1);
        redisTemplate.opsForValue().set(key, cacheSmsDto, DateUtil.getNaturalDayRemainingSeconds(), TimeUnit.SECONDS);

        return ResultVO.okMsgData("发送成功", random);
    }
    @Override
    public boolean checkSendMsg(SmsBean smsBean){
        log.info("校验短信验证码,phone=" + smsBean.getPhone() + ",smsType=" + smsBean.getYzmType() + " userType=" + smsBean.getUserType());
        int ret = smsMapper.updateMsgCheckCount(smsBean);
        if (ret != 1) {
            log.info("更新短信验证码校验次数失败 phone=" + smsBean.getPhone() + " smsType=" + smsBean.getYzmType() + " userType=" + smsBean.getUserType());
            throw new UserException(ResultEnum.SMSYZM_CHECK_ERROR.getCode(), "验证码错误");
        }

        ret = smsMapper.checkInvalidMsg(smsBean);
        if (ret != 1) {
            throw new UserException(ResultEnum.USER_MSG_INVALID_ERROR.getCode(), "短信验证码超时");
        }
        ret = smsMapper.updateMsgVerify(smsBean);
        if (ret != 1) {
            log.info("短信验证码错误,phone=" + smsBean.getPhone() + " smsType=" + smsBean.getYzmType() + " userType=" + smsBean.getUserType());
            throw new UserException(ResultEnum.SMSYZM_CHECK_ERROR.getCode(), "验证码错误");
        }
        log.info("短信验证码正确,phone=" + smsBean.getPhone() + ",smsType=" + smsBean.getYzmType());
        return true;
    }

}
